package com.zhaolang.qogir.ui.adapter.manage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhaolang.qogir.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ManageClassSpinnerAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> spinnerList;

    public ManageClassSpinnerAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<String> spinnerList) {
        this.spinnerList = spinnerList;
    }

    @Override
    public int getCount() {
        if(spinnerList == null){
            return 0;
        }else{
            return spinnerList.size();
        }
    }

    @Override
    public String getItem(int position) {
        return spinnerList != null && position >= 0 & position < spinnerList.size() ? spinnerList
                .get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final String cls = getItem(position);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.ui_manage_spinner_items, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (cls.equals("")) {
            holder.manageTvSpinnerClass.setText("");
        } else {
            holder.manageTvSpinnerClass.setText(cls);
        }
        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.manage_tv_spinner_class)
        TextView manageTvSpinnerClass;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
